/**
 * @Description: 路由配置
 * @Author 卞鹏飞 <228443632@qq.com>
 * @create 02/08/24 5:10 PM
 */

import type { RouteRecordRaw } from 'vue-router'
import DefaultLayout from '@/layout/default/index.vue'
import profile from '@root/profile.ts'

/**
 * 模块类型
 */
export const MODULE_TYPE = {
  /**
   * 服务商
   */
  SERVICE_PROVIDER: 'service-provider'
}

/**
 * 微前端架加载应用
 * @type {{path: string, component: function(): Promise<{}>, meta: {cache: boolean, title: *}}[]}
 */
const microRoutes = (profile.apps || []).map((v) => {
  // tmpRes
  return {
    path: `/${v.base}/:pathMatch(.*)*`,
    meta: { cache: true, title: v.title, iframe: true },
    component: () => import('@/view/micro-view/index.vue')
  }
})

/**
 * 微前端架-流程引擎加载应用
 * @type {{path: string, component: function(): Promise<{}>, meta: {cache: boolean, title: *}}[]}
 */
const microWorkflowRoutesIframe = [
  {
    path: `/micro-view/workflow/:pathMatch(.*)*`,
    meta: { cache: true, title: '流程引擎', iframe: true },
    component: () => import('@/view/micro-view/workflow/index.vue')
  }
]

/**
 * 全屏路由
 * 这里里面请务必 使用 /fullscreen开头
 */
const fullscreenRoutes: RouteRecordRaw[] = [
  {
    path: '/fullscreen/service-provider-reg',
    meta: { title: '个人业务服务商报名' },
    component: () => import('@/view/fullscreen/service-provider-reg/index.vue')
  }
]

/**
 * 异步路由
 */
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    meta: { title: '登录' },
    component: () => import('@/view/login/index.vue')
  },
  {
    path: '/login/forget-pass',
    meta: { title: '登录-忘记密码？' },
    component: () => import('@/view/login/forget-pass/index.vue')
  },
  {
    path: '/',
    redirect: '/home',
    meta: {},
    component: DefaultLayout,
    children: [
      ...microRoutes,
      ...microWorkflowRoutesIframe,
      {
        path: '/home',
        meta: { title: '首页' },
        component: () => import('@/view/home/index.vue')
      },

      /** **************************项目管理***************************** */
      {
        path: '/project/approval',
        meta: { title: '项目管理' },
        component: () => import('@/view/project/approval/index.vue')
      },
      {
        path: '/project/approval/edit',
        meta: { title: '编辑项目' },
        component: () => import('@/view/project/approval/components/edit/index.vue')
      },
      {
        path: '/project/approval/detail',
        meta: { title: '查看项目' },
        component: () => import('@/view/project/approval/components/detail/index.vue')
      },
      /** **************************example***************************** */
      {
        path: '/error/404',
        meta: { title: '404', iframe: true },
        component: () => import('@/view/error/404/index.vue')
      },
      {
        path: '/error/401',
        meta: { title: '401' },
        component: () => import('@/view/error/401/index.vue')
      },
      {
        path: '/to-do-dev',
        meta: { title: '待开发中...', group: true },
        component: () => import('@/view/to-do-dev/index.vue'),
        children: [
          {
            path: '/to-do-dev/dev-detail',
            meta: { title: '待开发中详情' },
            component: () => import('@/view/to-do-dev/DevDetail.vue')
          }
        ]
      },

      /* 示例 */
      {
        path: '/examples/test',
        meta: { title: 'test' },
        component: () => import('@/view/examples/test/index.vue')
      },
      {
        path: '/examples/el-table',
        meta: { title: 'el-table' },
        component: () => import('@/view/examples/el-table/index.vue')
      },
      {
        path: '/examples/base-query-table',
        meta: { title: 'base-query-table' },
        component: () => import('@/view/examples/base-query-table/index.vue')
      },
      {
        path: '/examples/base-query-filter',
        meta: { title: 'base-query-table' },
        component: () => import('@/view/examples/base-query-filter/index.vue')
      },
      {
        path: '/examples/base-highlight',
        meta: { title: 'base-highlight' },
        component: () => import('@/view/examples/base-highlight/index.vue')
      },
      {
        path: '/examples/base-form',
        meta: { title: 'base-form' },
        component: () => import('@/view/examples/base-form/index.vue')
      },
      {
        path: '/examples/base-upload',
        meta: { title: 'base-upload' },
        component: () => import('@/view/examples/base-upload/index.vue')
      },
      {
        path: '/examples/dictionary',
        meta: { title: 'dictionary' },
        component: () => import('@/view/examples/dictionary/index.vue')
      },
      {
        path: '/examples/codemirror',
        meta: { title: 'codemirror' },
        component: () => import('@/view/examples/codemirror/index.vue')
      },
      {
        path: '/examples/base-area-cascader',
        meta: { title: 'base-area-cascader' },
        component: () => import('@/view/examples/base-area-cascader/index.vue')
      },
      {
        path: '/examples/base-flow',
        meta: { title: 'base-flow' },
        component: () => import('@/view/examples/base-flow/index.vue')
      },
      {
        path: '/:pathMatch(.*)*',
        meta: {
          cache: false,
          title: '访问的页面不存在'
        },
        component: () => import('@/view/to-do-dev/index.vue')
      }
    ]
  },
  ...fullscreenRoutes
]

/**
 * 开发路由，不受权限控制
 */
export const publicRoutes: RouteRecordRaw[] = [
  {
    path: '/authorization',
    meta: { title: '授权中间件页面' },
    component: () => import('@/view/authorization/index.vue')
  }
]

/**
 * 全部路由
 */
export default [...asyncRoutes, ...publicRoutes]

/**
 * 白名单
 * @type {string[]}
 */
export const whiteList = ['/authorization', '/login', '/login/forget-pass'].concat(
  publicRoutes.filter((v) => v?.path !== '/').map((v) => v.path)
)

/**
 * 公共路由
 * @type {string[]}
 */
export const publicPaths = [].map((v) => {
  const matches = v.match(/(.*?\/):(\w+)/)
  return matches?.length ? new RegExp(`^${matches[1]}`) : new RegExp(`^${v}$`)
})
